
function updateMenu(){
    //初始化
    $('.subMenu').hide();
    $('.subMenu').show();

    $('li.active>.subMenu').show();
    //给菜单项添加事件
    $('.navMenu a').click(function() {
        //获取所属列表ul
        var $subMenuElement = $(this).next();
        var $liElement = $(this).parent();
        var $ulElement = $(this).parent().parent();
        //没有子菜单，则直接返回
        if (!$subMenuElement.is('ul')) {
            $ulElement.find('li').removeClass('active');
            $ulElement.find('ul.subMenu').slideUp();
            $liElement.addClass('active');
            return;
        }
        //如果存在子菜单，则打开或者关闭
        if (!$liElement.hasClass('active')) {
            $ulElement.find('li').removeClass('active');
            $ulElement.find('ul.subMenu').slideUp();
            $liElement.addClass('active');
            $subMenuElement.slideDown();
        } else {
            //打开状态 则关闭本菜单
            $subMenuElement.slideUp();
            $liElement.removeClass('active');
        }
    });
}

$(function() {
    updateMenu();
});
$(document).ready(
    function () {
        htmlobj = $.ajax({
                method: "POST",
                url: "http://127.0.0.1/api/public/Doc/docType",
                async: false,
                dataType: "json",
                beforeSend: function (xhr) {
                    //xhr.setRequestHeader("x-requested-with","DESKTOP_WEB");
                },
                success: function (data) {
                    // 获取每种分类
                    if (data['code'] == 200) {
                        obj = JSON.parse(data['data']);
                        Object.keys(obj).forEach(function (key) {
                            //$("#navMenu").style.border = "3px solid green";
                            $("#navMenu").append('<li id="doc_type_' + key + '"><a href="#" id="arrow'  + '">' + obj[key] + '</a></li>');
                            // 获取每种分组下的文章列表
                            $.ajax({
                                method: "POST",
                                url: "http://127.0.0.1/api/public/Doc/docs",
                                async: false,
                                dataType: "json",
                                data: JSON.stringify({id:parseInt(key,10)}),
                                contentType: "application/json", //必须这样写
                                beforeSend: function (xhr) {
                                    //xhr.setRequestHeader("x-requested-with","DESKTOP_WEB");
                                },
                                success: function (data) {
                                    objs = data['data'];
                                    // 获取每种分类
                                    if (data['code'] == 200) {
                                        $("#doc_type_" + key).append('<ul class="subMenu" id = "type_id_' + key
                                            + '" style="display: none"></ul>');
                                        Object.keys(objs).forEach(function (doc_id) {
                                            let rand = parseInt(Math.random() * 10000 %10000);
                                            $("#type_id_" + key).append('<li><a ' + 'id=' + rand + '>'+ objs[doc_id].title + '</a></li>');
                                            console.log(doc_id,objs[doc_id].title);
                                            // 注册回调s
                                            // console.log("#type_id_0 li a #" + rand);
                                            $("#" + rand).on("click",(function () {
                                                console.log($("#" + rand).html());
                                                $(location).attr('href', "http://127.0.0.1/api/public/Blog/article?title=" + $("#" + rand).html());
                                            }));
                                        })

                                    }
                                }
                            }
                        );
                    });
                }
            }
        })
        updateMenu();
    }
    );
